<?php
//节点模块
class OptsAction extends CommonAction {
	public function _filter(&$map)
	{
		$map['group_id']  = array('eq',0); 
		if(isset($map['pid']) && $map['pid']!=-1){
		   $pid=$map['pid'];
		   $this->assign('pid',intval($pid));
		   $map['pid']  = array('eq',$map['pid']);   
		}else{
		   $map['pid']  = array('egt',0); 
		}
        $map['level']  = array('eq',3); 
		$this->assign('level',3);
		//获取上级节点
		$add=Array('add','foreverdelete','edit');//需要检测的操作是否有权限 
		$add=$this->CheckAccess($add);
		$this->assign('opt',$add);
	}
	
	public function index() {
		//列表过滤器，生成查询Map对象
		$map = $this->_search ();
		if (method_exists ( $this, '_filter' )) {
			$this->_filter ( $map );
		}
		$name='Node';
		$model = D ($name);
		if (! empty ( $model )) {
			$this->_list ( $model, $map );
		}
		//搜索框
	    $p_list=$model->query('select  pid from '.C('DB_PREFIX').'Node where group_id=0 and `level`=3 group by pid');
		$this->assign('p_list',$p_list);

		$this->display ();
		return;
	}
	public function _before_index() {
		$model	=	M("Group");
		$list	=	$model->where('status=1')->getField('id,title');
		$this->assign('groupList',$list);
	}
	// 获取配置类型
	public function _before_add() {
		$model	=	M("Node");
		$list	=	$model->where('status=1 and pid=1')->select();
		$this->assign('list',$list);

		$this->assign('level',3);//$node->level+1
		$this->assign('WEB_FILE',C('WEB_FILE'));
		
	}
	public function _before_insert() {
		$this->insert('Node');
	}
    public function _before_patch(){
		$model	=	M("Group");
		$list	=	$model->where('status=1')->select();
		$this->assign('list',$list);
		$node	=	M("Node");
		$node->getById($_SESSION['currentNodeId']);
        $this->assign('pid',$node->id);
		$this->assign('level',$node->level+1);
    }
	public function _before_edit() {
		$model	=	M("Node");
		$list	=	$model->where('status=1 and pid=1')->select();
		$this->assign('list',$list);
	
		$this->assign('level',3);
	}
	public function _before_forbid() {
	    $this->forbid('Node');
	}
	public function _before_resume() {
	    $this->resume('Node');
	}
    /**
     +----------------------------------------------------------
     * 默认排序操作
     +----------------------------------------------------------
     * @access public
     +----------------------------------------------------------
     * @return void
     +----------------------------------------------------------
     */
    public function sort()
    {
		$node = M('Node');
        if(!empty($_GET['sortId'])) {
            $map = array();
            $map['status'] = 1;
            $map['id']   = array('in',$_GET['sortId']);
            $sortList   =   $node->where($map)->order('sort asc')->select();
        }else{
            if(!empty($_GET['pid'])) {
                $pid  = $_GET['pid'];
            }else {
                $pid  = $_SESSION['currentNodeId'];
            }
            if($node->getById($pid)) {
                $level   =  $node->level+1;
            }else {
                $level   =  1;
            }
            $this->assign('level',$level);
            $sortList   =   $node->where('status=1 and pid='.$pid.' and level='.$level)->order('sort asc')->select();
        }
        $this->assign("sortList",$sortList);
        $this->display();
        return ;
    }

	public function _before_foreverdelete() {
		//删除指定记录
		$this->foreverdelete('Node');
	}
	
	public function _before__search() {
		$this->_search('Node');
	}
	function update($name='Node') {
		//B('FilterString');
		$name=empty($name)?$this->getActionName():$name;
		$model = D ( $name );
		if (false === $model->create ()) {
			$this->error ( $model->getError () );
		}
		// 更新数据
		$list=$model->save ();
		if (false !== $list) {
			//成功提示
			$this->assign ( 'jumpUrl', Cookie::get ( '_currentUrl_' ) );
			$this->success ('编辑成功!');
		} else {
			//错误提示
			$this->error ('编辑失败!');
		}
	}
	function edit($name='Node') {
		$name=empty($name)?$this->getActionName():$name;
		$model = M ( $name );
		$id = $_REQUEST [$model->getPk ()];
		$vo = $model->getById ( $id );
		$this->assign ( 'vo', $vo );
		$this->display ();
	}
	/*public function _before_update() {
		$this->update('Node');
	}*/
   
}